package com.example.bloom.myapplication;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.List;

/**
 * Created by bloom on 2017/5/31.
 * 回复adapter
 */

public class ReplyMessageAdapter extends RecyclerView.Adapter<ReplyMessageAdapter.ViewHolder> {

    private Context mContext;
    private List<ReportReplyBean> mDatas;

    public ReplyMessageAdapter(Context context, List<ReportReplyBean> lists) {
        this.mContext = context;
        this.mDatas = lists;
    }

    @Override
    public ReplyMessageAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View contentView = LayoutInflater.from(mContext).inflate(R.layout.layout_item_relay_view, parent, false);
        ViewHolder holder = new ViewHolder(contentView);
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {

        if (mDatas != null && mDatas.size() > 0) {

            ReportReplyBean reply = mDatas.get(position);

            holder.tvReplier.setText((reply.getUserName() != null && !reply.getUserName().equals("")) ? reply.getUserName() : "");
            holder.tvReplytime.setText((reply.getReplyTime() != null && !reply.getReplyTime().equals("")) ? reply.getReplyTime() : "");
            holder.tvReplyContent.setText((reply.getReplyContent() != null && !reply.getReplyContent().equals("")) ? reply.getReplyContent() : "");
            holder.itemView.setTag(reply);
        }
    }

    public void addRelay(ReportReplyBean reply) {
        mDatas.add(getItemCount(), reply);
        notifyItemInserted(getItemCount() + 1);
    }

    @Override
    public int getItemCount() {
        return mDatas == null ? 0 : mDatas.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        TextView tvReplier;
        TextView tvReplytime;
        TextView tvReplyContent;

        public ViewHolder(View itemView) {
            super(itemView);
            tvReplier = (TextView) itemView.findViewById(R.id.tv_replier);
            tvReplytime = (TextView) itemView.findViewById(R.id.tv_reply_time);
            tvReplyContent = (TextView) itemView.findViewById(R.id.tv_reply_content);
        }
    }
}
